<template>
  <el-dialog v-model="visible" :title="`${paramsProps.title}`" :destroy-on-close="true" width="580px" draggable>
    <el-form
      ref="ruleFormRef"
      label-width="140px"
      label-suffix=" :"
      :rules="rules"
      :model="paramsProps.row"
      @submit.enter.prevent="handleSubmit"
    >
      <el-form-item label="用户名" prop="username">
        <el-input v-model="paramsProps.row.username" placeholder="请填写用户名" clearable></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="pwd">
        <el-input v-model="paramsProps.row.pwd" placeholder="请填写密码" clearable></el-input>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input v-model="paramsProps.row.phone" placeholder="请填写手机号" clearable></el-input>
      </el-form-item>
      <el-form-item label="昵称" prop="nickname">
        <el-input v-model="paramsProps.row.nickname" placeholder="请填写昵称" clearable></el-input>
      </el-form-item>
      <el-form-item label="性别(0 未知 1 男 2 女)" prop="sex">
        <el-select v-model="paramsProps.row.sex" clearable placeholder="请选择性别(0 未知 1 男 2 女)">
          <el-option
            v-for="item in optionsStore.getDictOptions('')"
            :key="item.id"
            :label="item.codeName"
            :value="Number(item.id)"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="生日" prop="birthday">
        <el-input v-model="paramsProps.row.birthday" placeholder="请填写生日" clearable></el-input>
      </el-form-item>
      <el-form-item label="头像地址" prop="logo">
        <el-input v-model="paramsProps.row.logo" placeholder="请填写头像地址" clearable></el-input>
      </el-form-item>
      <el-form-item label="年龄，--废弃，以生日为主" prop="age">
        <el-input-number v-model="paramsProps.row.age" :precision="0" :min="1" :max="999999" />
      </el-form-item>
      <el-form-item label="身份证" prop="idCard">
        <el-input v-model="paramsProps.row.idCard" placeholder="请填写身份证" clearable></el-input>
      </el-form-item>
      <el-form-item label="邮箱地址" prop="email">
        <el-input v-model="paramsProps.row.email" placeholder="请填写邮箱地址" clearable></el-input>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="visible = false"> 取消</el-button>
      <el-button type="primary" @click="handleSubmit"> 确定</el-button>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive } from 'vue';
import { type ElForm, ElMessage } from 'element-plus';
import { useOptionsStore } from '@/stores/modules/options';

defineOptions({
  name: 'SysUserEmployeeForm'
});

const optionsStore = useOptionsStore();
const rules = reactive({
  username: [{ required: true, message: '请填写用户名' }],
  pwd: [{ required: true, message: '请填写密码' }]
});

const visible = ref(false);
const paramsProps = ref<View.DefaultParams>({
  title: '',
  row: {},
  api: undefined,
  getTableList: undefined
});

// 接收父组件传过来的参数
const acceptParams = (params: View.DefaultParams) => {
  paramsProps.value = params;
  visible.value = true;
};

// 提交数据（新增/编辑）
const ruleFormRef = ref<InstanceType<typeof ElForm>>();
const handleSubmit = () => {
  ruleFormRef.value!.validate(async valid => {
    if (!valid) return;
    try {
      await paramsProps.value.api!(paramsProps.value.row);
      ElMessage.success({ message: `${paramsProps.value.title}成功！` });
      paramsProps.value.getTableList!();
      visible.value = false;
    } catch (error) {
      console.log(error);
    }
  });
};

defineExpose({
  acceptParams
});
</script>

<style scoped lang="scss"></style>
